home *** CD-ROM | disk | FTP | other *** search
/ LightWavin' Magazine 3 / LightWavin' Magazine, Issue 3 (LightWavin' Multimedia)(1997).iso / alphac~1 / intel / alphclip.c next >
C/C++ Source or Header  |  1997-03-25  |  7KB  |  353 lines

  1. /*
  2.  * ALPHCLIP.C -- Layout Plugin Image Filter
  3.  *
  4.  *        This plugin will clip out various portions of the alpha
  5.  *        channel of an image.
  6.  *
  7.  * Version 1.00, by Brenden Mecleary
  8.  * last revision 3/23/97
  9.  *
  10.  *
  11.  */
  12.  
  13. #include <splug.h>
  14. #include <moni.h>
  15. #include <lwran.h>
  16. #include <lwpanel.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20.  
  21.  
  22. /* These globals make adding controls easier. */
  23. static LWPanControlDesc   desc;                                 
  24. static LWValue ival={LWT_INTEGER},ivecval={LWT_VINT},
  25.   fval={LWT_FLOAT},fvecval={LWT_VFLOAT},sval={LWT_STRING};
  26.  
  27.  
  28. /* This structure will hold all the necessary information for the clipping. */
  29. typedef struct AACLIPINFO_TAG{
  30.     int nRfill, nGfill, nBfill, nClipThreshold, nSideFlag;
  31. } AACLIPINFO;
  32.  
  33.  
  34. /* The damn thing doesn't just accept int values, so I had to make
  35.  * my own structure to hold settings for the panel. */
  36. typedef struct MYPANINFO_TAG{
  37.     int x, y;
  38. } MYPANINFO;
  39.  
  40.  
  41. /*
  42.  * FUNCTIONS
  43.  */
  44.  
  45.     XCALL_(static LWInstance)
  46. ACCreate (LWError *err)
  47. {
  48.     AACLIPINFO *aaci;
  49.  
  50.     XCALL_INIT;
  51.     aaci = calloc(sizeof(struct AACLIPINFO_TAG),1);
  52.  
  53.     aaci->nClipThreshold = 255;
  54.     aaci->nSideFlag = 0;
  55.     aaci->nRfill = 0;
  56.     aaci->nGfill = 255;
  57.     aaci->nBfill = 0;
  58.  
  59.     return ((LWInstance) aaci);
  60. }
  61.  
  62.     XCALL_(void)
  63. ACDestroy (LWInstance inst)
  64. {
  65.     XCALL_INIT;
  66.     if (inst)
  67.         free(inst);
  68. }
  69.  
  70.     XCALL_(static LWError)
  71. ACCopy (
  72.     LWInstance         from,
  73.     LWInstance         to)
  74. {
  75.     AACLIPINFO *old, *new;
  76.     old = (AACLIPINFO *) from;
  77.     new = (AACLIPINFO *) to;
  78.     *new = *old;
  79.     return (NULL);
  80. }
  81.  
  82.     XCALL_(static LWError)
  83. ACLoad (
  84.     LWInstance         inst,
  85.     const LWLoadState    *ls)
  86. {
  87.     AACLIPINFO *aaci;
  88.     char line[80];
  89.  
  90.     XCALL_INIT;
  91.  
  92.     aaci = (AACLIPINFO *) inst;
  93.     (*ls->read)(ls->readData,line,80);
  94.  
  95.     sscanf(line,"%i %i %i %i %i",
  96.                     &aaci->nRfill,
  97.                     &aaci->nGfill,
  98.                     &aaci->nBfill,
  99.                     &aaci->nClipThreshold,
  100.                     &aaci->nSideFlag);
  101.     return (NULL);
  102. }
  103.  
  104.     XCALL_(static LWError)
  105. ACSave (
  106.     LWInstance         inst,
  107.     const LWSaveState    *ss)
  108. {
  109.     AACLIPINFO *aaci;
  110.  
  111.     char line[80];
  112.  
  113.     XCALL_INIT;
  114.  
  115.     aaci = (AACLIPINFO *) inst;
  116.  
  117.     sprintf(line,"%i %i %i %i %i",
  118.                     aaci->nRfill,
  119.                     aaci->nGfill,
  120.                     aaci->nBfill,
  121.                     aaci->nClipThreshold,
  122.                     aaci->nSideFlag);
  123.     (*ss->write)(ss->writeData,line,80);
  124.     return (NULL);
  125. }
  126.  
  127.  
  128.  
  129. /*
  130.  * PROCESSING
  131.  */
  132.     XCALL_(static void)
  133. ACClipProcess (
  134.     LWInstance         inst,
  135.     const FilterAccess    *fa)
  136. {
  137.     AACLIPINFO *aaci;
  138.  
  139.     Monitor            *mon;
  140.     BufferValue         out[4], *r, *g, *b, *a;
  141.     int             i, j;
  142.  
  143.     
  144.     aaci = (AACLIPINFO *) inst;
  145.     mon = fa->monitor;
  146.  
  147.     /*
  148.      * Scan through the lines in the image and get image buffer
  149.      * line pointers for the RGB and alpha buffers of the original
  150.      * image.
  151.      */
  152.     MON_INIT (mon, fa->height);
  153.     for (i = 0; i < fa->height; i++) {
  154.         r = (*fa->bufLine) (LWBUF_RED,   i);
  155.         g = (*fa->bufLine) (LWBUF_GREEN, i);
  156.         b = (*fa->bufLine) (LWBUF_BLUE,  i);
  157.         a = (*fa->bufLine) (LWBUF_ALPHA, i);
  158.  
  159.         /* Loops through each pixel in the current line */
  160.         for (j = 0; j < fa->width; j++) {
  161.  
  162.  
  163.         /*
  164.          * For each pixel in the current line, key off the alpha
  165.          * channel to determine if the pixel should simply be copied,
  166.          * or replaced with the background "fill" color.
  167.          */
  168.  
  169.             switch(aaci->nSideFlag)
  170.             {
  171.             case 1:
  172.                 if(a[j] > aaci->nClipThreshold)
  173.                 {
  174.                     out[0] = aaci->nRfill;
  175.                     out[1] = aaci->nGfill;
  176.                     out[2] = aaci->nBfill;
  177.                     out[3] = a[j];
  178.                 }
  179.                 else
  180.                 {
  181.                     out[0] = r[j];
  182.                     out[1] = g[j];
  183.                     out[2] = b[j];
  184.                     out[3] = a[j];
  185.                 }
  186.                 break;
  187.  
  188.             default:
  189.                 if(a[j] < aaci->nClipThreshold)
  190.                 {
  191.                     out[0] = aaci->nRfill;
  192.                     out[1] = aaci->nGfill;
  193.                     out[2] = aaci->nBfill;
  194.                     out[3] = a[j];
  195.                 }
  196.                 else
  197.                 {
  198.                     out[0] = r[j];
  199.                     out[1] = g[j];
  200.                     out[2] = b[j];
  201.                     out[3] = a[j];
  202.                 }
  203.                 break;
  204.             }
  205.  
  206.             (*fa->setRGB)   (j, i, out);
  207.             (*fa->setAlpha) (j, i, out[3]);
  208.         }
  209.  
  210.         /* Step the monitor on every 10th go...I think. */
  211.         if ((i % 10) == 0)
  212.         {
  213.             if (MON_INCR (mon, 10))
  214.                 return;
  215.         }
  216.     }
  217.  
  218.     MON_DONE (mon);
  219. }
  220.  
  221.  
  222.  
  223. /*
  224.  * FLAGS
  225.  */
  226.     XCALL_(static unsigned int)
  227. ACClipFlags (
  228.     LWInstance         inst)
  229. {
  230.     return (0);
  231. }
  232.  
  233.  
  234. /*
  235.  * ACTIVATION
  236.  */
  237.     XCALL_(int)
  238. ACActivate (
  239.     long             version,
  240.     GlobalFunc        *global,
  241.     ImageFilterHandler    *local,
  242.     void            *serverData)
  243. {
  244.     XCALL_INIT;
  245.     if (version != 1)
  246.         return (AFUNC_BADVERSION);
  247.  
  248.     local->create  = ACCreate;
  249.     local->destroy = ACDestroy;
  250.     local->load    = ACLoad;
  251.     local->save    = ACSave;
  252.     local->copy    = ACCopy;
  253.  
  254.     local->process = ACClipProcess;
  255.     local->flags   = ACClipFlags;
  256.  
  257.     return (AFUNC_OK);
  258. }
  259.  
  260.  
  261. /*
  262.  * INTERFACE
  263.  */
  264.     XCALL_(static int)
  265. ACInterface (
  266.     long             version,
  267.     GlobalFunc        *global,
  268.     AACLIPINFO        *inst,
  269.     void            *serverData)
  270. {
  271.     LWPanelFuncs    *panl;
  272.     LWPanelID        panID;
  273.     
  274.     LWControl        *info;
  275.     MessageFuncs    *message;
  276.  
  277.     MYPANINFO mypi;
  278.  
  279.     char *cPopupChoices[][17] = {"MORE transparent", "LESS transparent"};
  280.  
  281.     /* Set up the panel structure values*/
  282.     mypi.x = 160;
  283.     mypi.y = 120;
  284.  
  285.     /* Lightwave-specific shit. */
  286.     XCALL_INIT;
  287.     message = (*global) ("Info Messages", GFUSE_TRANSIENT);
  288.     if (!message )
  289.         return AFUNC_BADGLOBAL;
  290.  
  291.     panl = (*global) (PANEL_SERVICES_NAME, GFUSE_TRANSIENT);
  292.     if(!panl)
  293.     {
  294.         (*message->error)("Unable to activate global "PANEL_SERVICES_NAME, "     please add plugin lwpanels.p" );
  295.         return AFUNC_BADGLOBAL;
  296.     }
  297.  
  298.  
  299.     /* Setting up the panel. */
  300.     panID = PAN_CREATE(panl,"AlphaClip Version 1.00 - ⌐ 1997, Brenden Mecleary");
  301.  
  302.     panl->set(panID, PAN_X, &mypi.x);
  303.     panl->set(panID, PAN_Y, &mypi.y);
  304.  
  305.     /* Adding controls onto the panel. */
  306.     RGB_CTL(panl, panID, "Fill color:");
  307.     SLIDER_CTL(panl, panID, "Threshold:", 200, 0, 255);
  308.     POPUP_CTL(panl, panID, "Clip mode:", cPopupChoices[0]);
  309.  
  310.  
  311.     /* Load instance data into the controls. */
  312.     info = panl->nextControl(panID, NULL);
  313.     SET_IVEC(info, inst->nRfill, inst->nGfill, inst->nBfill);
  314.     info = panl->nextControl(panID, info);
  315.     SET_INT(info, inst->nClipThreshold);
  316.     info = panl->nextControl(panID, info);
  317.     if(inst->nSideFlag == 1)
  318.         SET_INT(info, 1);
  319.     else
  320.         SET_INT(info, 0);
  321.  
  322.  
  323.     /* Draw the panel. */
  324.     panl->open(panID, PANF_BLOCKING);
  325.  
  326.  
  327.     /* Save the panel information. */
  328.     info = panl->nextControl(panID, NULL);
  329.     GET_IVEC(info, inst->nRfill, inst->nGfill, inst->nBfill);
  330.     info = panl->nextControl(panID, info);
  331.     GET_INT(info, inst->nClipThreshold);
  332.     info = panl->nextControl(panID, info);
  333.     GET_INT(info, inst->nSideFlag);
  334.  
  335.  
  336.     /* Destroying the panel. */
  337.     PAN_KILL(panl,panID);
  338.     
  339.     return (AFUNC_OK);
  340. }
  341.  
  342.     
  343.  
  344. /*
  345.  * Globals used to identify this plugin to Lightwave.
  346.  */
  347.  
  348. ServerRecord ServerDesc[] = {
  349.     { "ImageFilterHandler",    "Bman's_AlphaClip",    ACActivate },
  350.     { "ImageFilterInterface",    "Bman's_AlphaClip",    ACInterface },
  351.     { NULL }
  352. };
  353.